﻿using ConfExpert.Forms.Base;
using ConfExpert.Utility;
using DevExpress.XtraReports.UI;
using DevTeam;
using System;
using System.Data;
using System.Windows.Forms;

namespace ConfExpert.Forms.Depo
{
    public partial class KumasIrsaliyeOlustur : XForm
    {

        //TODO OA: ParcaCikisEmirleri'ne uyarla!
        string CikanPartiIds = null;
        int nextIrsaliye, IrsaliyeId = 0;
        string CikisEmriId = string.Empty;
        char Tip = 'C';
        public KumasIrsaliyeOlustur()
        {
            InitializeComponent();
        }

        public KumasIrsaliyeOlustur(string CikisEmriId)
            : this()
        {
            this.CikisEmriId = CikisEmriId;
        }

        public KumasIrsaliyeOlustur(int IrsaliyeId)
            : this()
        {
            this.IrsaliyeId = IrsaliyeId;
        }

        public KumasIrsaliyeOlustur(string CikisEmriId, char Tip)
            : this(CikisEmriId)
        {
            this.Tip = Tip;
        }

        private void KumasIrsaliyeOlustur_Load(object sender, EventArgs e)
        {
            gleCari.Properties.Assign(RI(Rp.Atelye));
            gleSevkiyatci.Properties.Assign(RI(Rp.Sevkiyatci));

            DataTable dt;
            if (Tip=='P' || IrsaliyeId>0)
            {
                dt = ExecuteDataTable("GetKumasIrsaliyeNo", IrsaliyeTipi.SelectedIndex);
            }
            else
            {
                dt = ExecuteDataTable("GetKumasIrsaliyeNo2", CikisEmriId);
            }
            DataRow dr = dt.Rows[0];

            txtIrsaliyeNo.Text = String.Format("{0:000000}", intParse(dr["IrsaliyeNumber"]) + 1);
            IrsaliyeTipi.SelectedIndex = dr["ShipType"].ToInt(-1);

            dateIrsaliye.EditValue = DateTime.Now;
            dateSevk.EditValue = DateTime.Now;

            if (CikisEmriId != "" && CikisEmriId != null)
            {
                DataTable dt2 = ExecuteDataTable("GetShipInformations2", CikisEmriId, Tip);
                if (dt2.Rows.Count > 1)
                {
                    ShowMessage("Seçtiğiniz çıkış emirlerinde farklı atölye bilgileri mevcut !");
                    this.DialogResult = DialogResult.Cancel;
                }

                DataRow dr2 = dt2.Rows[0];
                if (intParse(dr2["AtelyeId"]) > 0)
                {
                    CariTipi.SelectedIndex = 0;
                    gleCari.EditValue = intParse(dr2["AtelyeId"]);
                }
                else if (intParse(dr2["CustomerId"]) > 0)
                {
                    CariTipi.SelectedIndex = 1;
                    gleCari.Properties.Assign(RI(Rp.V_Customer));
                    gleCari.EditValue = intParse(dr2["CustomerId"]);
                }
                else if (intParse(dr2["VendorId"]) > 0)
                {
                    CariTipi.SelectedIndex = 2;
                    gleCari.Properties.Assign(RI(Rp.Vendor));
                    gleCari.EditValue = intParse(dr2["VendorId"]);
                }
                if (intParse(dr2["AtelyeId"]) > 0 || intParse(dr2["CustomerId"]) > 0 || intParse(dr2["VendorId"]) > 0)
                {
                    CariTipi.Properties.ReadOnly = true;
                    gleCari.Properties.ReadOnly = true;
                }
            }

            btnOk.Click += btnOk_Click;
            CariTipi.SelectedIndexChanged += cmbTip_SelectedIndexChanged;
            IrsaliyeTipi.SelectedIndexChanged += cbTip_SelectedIndexChanged;
            btnCancel.Click += btnCancel_Click;
            btnKoli.Click += btnKoli_Click;
        }

        void btnKoli_Click(object sender, EventArgs e)
        {
            using (AksesuarCikisPartileri ap = new AksesuarCikisPartileri())
                if (ap.ShowDialog() == DialogResult.OK)
                    CikanPartiIds = ap.CikanPartiIds;
        }

        void btnCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
        }

        void cbTip_SelectedIndexChanged(object sender, EventArgs e)
        {
            DataTable dt = ExecuteDataTable("GetKumasIrsaliyeNo", IrsaliyeTipi.SelectedIndex);
            DataRow dr = dt.Rows[0];
            txtIrsaliyeNo.Text = String.Format("{0:000000}", intParse(dr["IrsaliyeNumber"]) + 1);
        }

        void cmbTip_SelectedIndexChanged(object sender, EventArgs e)
        {
            gleCari.EditValue = null;

            switch (CariTipi.SelectedIndex)
            {
                case 0:
                    gleCari.Properties.Assign(RI(Rp.Atelye));
                    break;
                case 1:
                    gleCari.Properties.Assign(RI(Rp.V_Customer));
                    break;
                case 2:
                    gleCari.Properties.Assign(RI(Rp.Vendor));
                    break;
            }
        }

        void btnOk_Click(object sender, EventArgs e)
        {
            if (intParse(gleCari.EditValue) == 0)
            {
                ShowMessage("Lütfen cari seçin !");
                return;
            }
            nextIrsaliye = (Convert.ToInt32(txtIrsaliyeNo.Text)) - 1;
            this.DialogResult = DialogResult.OK;

            Rapor.KumasIrsaliye irs;
            Rapor.xrIrsaliye irsDetay;
            string TeslimEden = string.Empty;

            int CariTip = CariTipi.SelectedIndex;
            int GleCariId = gleCari.EditValue.ToInt();

            if (gleSevkiyatci.EditValue != null)
                TeslimEden = AppSession.Lookups.Sevkiyatci.FindBySevkiyatciId(intParse(gleSevkiyatci.EditValue)).Name;
           
            if (CikisEmriId != "" && CikisEmriId != null && (Tip=='C' || Tip=='S'))
            {
                irs = new Rapor.KumasIrsaliye(txtIrsaliyeNo.Text, dateSevk.DateTime, txtNot.Text, CikisEmriId, WhoAmI, CariTip, GleCariId, CikanPartiIds, TeslimEden, "GetKumasIrsaliyeByCikisEmriId");
                irsDetay = new Rapor.xrIrsaliye(CikisEmriId, 0, "GetCikisEmri");
            }
            else if (CikisEmriId != "" && CikisEmriId != null && Tip == 'P')
            {
                irs = new Rapor.KumasIrsaliye(txtIrsaliyeNo.Text, dateSevk.DateTime, txtNot.Text, CikisEmriId, WhoAmI, CariTip, GleCariId, CikanPartiIds, TeslimEden, "GetKumasIrsaliyeByParcaCikisEmriId");
                irsDetay = new Rapor.xrIrsaliye(CikisEmriId, 0,"GetParcaCikisEmri");
            }
            else
            {
                irs = new Rapor.KumasIrsaliye(txtIrsaliyeNo.Text, dateSevk.DateTime, txtNot.Text, IrsaliyeId, WhoAmI, CariTip, GleCariId, CikanPartiIds, TeslimEden);
                irsDetay = new Rapor.xrIrsaliye(IrsaliyeId, 0);
            }

            irs.PrintProgress += irs_PrintProgress;
            irs.PrintingSystem.EndPrint += PrintingSystem_EndPrint;
            irs.PrintingSystem.StartPrint += PrintingSystem_StartPrint;
            irs.PrinterName = "SAMSUNG ML 2550";
            irsDetay.PrinterName = "Xerox Plaser 6180n Pcl6";

            irs.ShowPreview();
            irsDetay.ShowPreview();
        }

        void PrintingSystem_EndPrint(object sender, EventArgs e)
        {
            Mail.MailTemplate template;

            string command = string.Empty;

            switch (Tip)
            {
                case 'C':
                    {
                        command = string.Format("SHOW ConfExpert.Forms.Depo.CikisEmirleri {0}", CikisEmriId);
                        template = Mail.GetTemplate(1, CikisEmriId);
                    }
                    break;
                
                case 'P':
                    {
                        command = string.Format("SHOW ConfExpert.Forms.Depo.ParcaCikisEmirleri {0}", CikisEmriId);
                        template = Mail.GetTemplate(2, CikisEmriId);
                    }
                    break;

                case 'S':
                    {
                        ExecuteNonQuery("UpdOwnerIdByCikisEmri", CikisEmriId);
                        command = string.Format("SHOW ConfExpert.Forms.Depo.CikisEmirleri {0}", CikisEmriId);
                        template = Mail.GetTemplate(3, CikisEmriId);
                    }
                    break;

                default: template = Mail.GetTemplate(1, CikisEmriId); break;
            }

            Mail.Send(template.To, template.Subject, template.Body, template.CC, command);
            this.DialogResult = DialogResult.OK;
        }

        void PrintingSystem_StartPrint(object sender, DevExpress.XtraPrinting.PrintDocumentEventArgs e)
        {
            e.PrintDocument.PrinterSettings.Copies = 4;
            e.PrintDocument.PrinterSettings.Collate = false;
        }
        
        void irs_PrintProgress(object sender, DevExpress.XtraPrinting.PrintProgressEventArgs e)
        {

            switch (Tip)
            {
                case 'C':
                    if (CikisEmriId != "" && CikisEmriId != null)
                    {
                        nextIrsaliye++;
                        Session.DataManager.ExecuteDataSet("createKumasIrsaliyeByCikisEmri", txtIrsaliyeNo.Text, dateSevk.DateTime.ToShortDateString(), txtNot.Text, nextIrsaliye, IrsaliyeTipi.SelectedIndex, CikisEmriId, CikanPartiIds);
                    }
                    else if (IrsaliyeId > 0)
                    {
                        nextIrsaliye++;
                        Session.DataManager.ExecuteDataSet("createKumasIrsaliyeByIrsaliyeId", txtIrsaliyeNo.Text, dateSevk.DateTime.ToShortDateString(), txtNot.Text, nextIrsaliye, IrsaliyeTipi.SelectedIndex, IrsaliyeId, CikanPartiIds, CariTipi.SelectedIndex, intParse(gleCari.EditValue));
                    }
                    break;

                case 'S':
                    if (CikisEmriId != "" && CikisEmriId != null)
                    {
                        nextIrsaliye++;
                        Session.DataManager.ExecuteDataSet("createKumasSatisIrsaliyeByCikisEmri", txtIrsaliyeNo.Text, dateSevk.DateTime.ToShortDateString(), txtNot.Text, nextIrsaliye, IrsaliyeTipi.SelectedIndex, CikisEmriId);
                    }
                    else if (IrsaliyeId > 0)
                    {
                        nextIrsaliye++;
                        Session.DataManager.ExecuteDataSet("createKumasIrsaliyeByIrsaliyeId", txtIrsaliyeNo.Text, dateSevk.DateTime.ToShortDateString(), txtNot.Text, nextIrsaliye, IrsaliyeTipi.SelectedIndex, IrsaliyeId, CikanPartiIds, CariTipi.SelectedIndex, intParse(gleCari.EditValue));
                    }
                    break;

                case 'P':
                    if (CikisEmriId != "" && CikisEmriId != null)
                    {
                        nextIrsaliye++;
                        Session.DataManager.ExecuteDataSet("createKumasIrsaliyeByParcaCikisEmri", txtIrsaliyeNo.Text, dateSevk.DateTime.ToShortDateString(), txtNot.Text, nextIrsaliye, IrsaliyeTipi.SelectedIndex, CikisEmriId, CikanPartiIds);
                    }
                    else if (IrsaliyeId > 0)
                    {
                        nextIrsaliye++;
                        Session.DataManager.ExecuteDataSet("createParcaKumasIrsaliyeByIrsaliyeId", txtIrsaliyeNo.Text, dateSevk.DateTime.ToShortDateString(), txtNot.Text, nextIrsaliye, IrsaliyeTipi.SelectedIndex, IrsaliyeId, CikanPartiIds, CariTipi.SelectedIndex, intParse(gleCari.EditValue));
                    }
                    break;
            }
        }
    }
}
